/**
 *
 * 防御士兵01
 * @author 
 *
 */
class AttackArcher01 extends AttackArcherBase{
    
    
    /**武器*/
    private weapon: Arrow01;
    //每次点击生成长度为1的路径寻路点
	public constructor() {
        super();
        this.view = new egret.MovieClip();
        this.addChild(this.view);
	}
    /**添加纹理 初始化数据*/
    public addTexture(tietu:string,name:string){
        this.nameInfo = name;
        var data = RES.getRes(tietu + "_json");
        var texture = RES.getRes(tietu + "_png");
        var mcf: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data,texture);
        this.view.movieClipData = mcf.generateMovieClipData(tietu);
        this.view.x = this.view.width / 2;
        this.anchorOffsetX = this.width / 2;
        //血条位置
        this.lifeBar.x = (this.view.width - this.lifeBar.width) / 2;
        this.lifeBar.y = -50;
        //初始不会变化的数据
        this._maxSpeed = 0.3;
        
        //this.damage = 20;
        //this.life = 40;
        this.offy = 10;
        
    }
	
    /**创建-初始化*/
    public onCreate(){
        this.posArr = [];
        this._pathIndex = 0;
        
        this.timesum = 0;
        this.lifeBar.reSet();
        this.canClear = false;
        
        this.atjijie = false;
        this.moveToTarget = false;       
        
        this.fsm.changeState(stateType.moveState);
        this.curState = stateType.moveState;
    }
    
    /**初始化 运动路径点 并移动*/
    public init(arr: number[][],life: number,damage: number):void {
        
        this.hp = this.life = life * (1 + 0.2 * GuankaBase.instance.lifeUp);
        this.damage = damage * (1 + 0.2 * GuankaBase.instance.damagekUp);
        
        this.atjijie = true;
        this._position.x = this.x = arr[0][0];
        this._position.y = this.y = arr[0][1];
        this.fsm.changeState(stateType.idleState);

    }
    
    public initRoad(arr: number[][]) {
        var i: number = 1;
        var len: number = arr.length;
        for(i;i < arr.length;i++) {
            this.jijieDian = new Vector2D(arr[i][0] + this.xoffset,arr[i][1] + this.yoffset);
            this.posArr.push(this.jijieDian);
        }
        this.fsm.changeState(stateType.moveState);
        this.ratioSoldY = this.minSoldRadius / this.maxSoldRadius;

    }
    
    /**播放射击音效*/
    protected playFireVoice() {
        if(Math.random() > 0.5)
            SoundManager.playEffect("arrow_fire1");
        else
            SoundManager.playEffect("arrow_fire2");
    }
    /**设置新的集结点*/
    public setPoint(arr:number[]){
        this.dispatchEvent(new SoldEvent(SoldEvent.MOVE,this,arr));
    }
    
    /**设置新的路径点 并移动*/
    public setPath(arr:number[]){
        this.atjijie = true;
        this.posArr.length = 0;
        this.jijieDian = new Vector2D(arr[0]+this.xoffset,arr[1]+this.yoffset);
        this.posArr.push(this.jijieDian);
        this.fsm.changeState(stateType.moveState);
        
        if(this.target == null)
            return;
        if(this.target.target != null){
            this.target.target = null;
        }
        this.target = null;
        //塔类侦听事件 这里发送消息
    }
    
    /**帧事件*/
    public onEnterFrame(advancedTime:number){
        super.onEnterFrame(advancedTime);
        
        //进入范围敌人数组为空则直接返回
        if(this.atargets.length == 0) {
            this.timesum = this.fireDelay;
            return;
        }
        
        //发射间隔
        this.timesum += advancedTime;
        if(this.timesum < this.fireDelay) {
            return;
        }
        this.timesum = 0;
        
        //射击音效播放
        this.playFireVoice();
        //轮流射击
        //利用对象池产生弓箭对象并进行碰撞检测
        this.weapon = <Arrow01>ObjectPool.getInstance().createObject(Arrow01);
        //this.weapon.damage = 4 * GuankaBase.instance.hardxs2;
        if(this.hp <= 0 || this.target == null){
            return;
        }
        this.weapon.damage = this.damage;
        var p1:egret.Point;
        p1 = new egret.Point(this.x,this.y-50);
        this.weapon.init(p1,this.target,this.target.offy);
        this.weapon.unit = this;
        GuankaBase.instance.weaponLayer.addChild(this.weapon);
        if(this.weapon.damage >= this.target.hp) {
            var idx: number = this.targets.indexOf(this.target);
            if(idx != -1)
                this.targets.splice(idx,1);
            idx = this.atargets.indexOf(this.target);
            if(idx != -1)
                this.atargets.splice(idx,1);
            this.target = null;
        }
               
    }
    
    /**鼠标选中对象*/
    private viewSelect(e:egret.TouchEvent){
        Group.selectItem(this);
    }
    
    /**销毁*/
    public onDestroy(): void {
        super.onDestroy();
        this.view.touchEnabled = false;
        this.view.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.viewSelect,this);
    }
    
}
